import { action, makeAutoObservable } from 'mobx';
import { request } from '@/request';

export type TopicsTab = '' | 'ask' | 'share' | 'job' | 'good' | 'dev';

export type GetTopicsParams = {
    [Prop in keyof TopicsStore]?: TopicsStore[Prop];
};

class TopicsStore {
    page: number = 1;

    tab: TopicsTab = '';

    limit: number = 10;

    mdrender: boolean = false;

    topics: any[] = [];

    constructor() {
        makeAutoObservable(this, {
            getTopics: action.bound,
        });
    }

    // 主题首页
    async getTopics(params: GetTopicsParams) {
        Object.assign(this, params);
        const res: any = await request.get('/api/v1/topics', {
            page: this.page,
            tab: this.tab,
            limit: this.limit,
            mdrender: this.mdrender,
            ...params,
        });
        this.topics = res.data;
    }
}

export default new TopicsStore();
